//Raj Kiran Akula
//CSC 363
//11-21-2009

#include <stdio.h>
#include <stlib.h>
void main()
{
	int cinput;      // Number used for user input
	int temp = 2;   // Used to check if current input number is divisible by a no less then input no.
	int error = 0;  // if user inputs a no. lower then 2 and not 0
	int prime = 0;  // if user input a no. that is prime this is set to 1
	int newinput;   // this is a temp variable to check for sub prime
	int sPrime = 0; // if user input a second order prime this is set to 1

	//Used to enter no to the user.
	printf("Enter a no.: ");
	scanf("%d", &cinput);
	
		__asm{
				mov eax, cinput // puts input in eax
				cmp eax, 2	   // compare eax to 2
				jge checkPrime // if eax is greater/equal then 2 jmp to check prime
				mov error, 1   // if input is 1 or less then 0 throws an error
				jmp coutput	   // helps complete do previous step
checkPrime:		mov ebx, temp  // moving temp to a register to be able to use temp
				cmp cinput, ebx// compare to see if input is greater then temp 
				jle coutput	   // if temp isgreater then we know no. is a prime and we jump out of program
				mov edx, 0	   // otherwise reset adx to zero
				mov eax, cinput // reset eax to be equal to input
				div temp	   // divide current temp with eax
				cmp edx, 0     // if there is no reminder
				je checkSPrimeS// then check for second order prime because currently its not a prime
				add temp, 1    // add 1 to temp if current temp is not divisible into input
				mov prime, 1   // so far from what values we have checked no is a prime
				jmp checkPrime // loop to check for next no can be divided ino input
checkSPrimeS:	mov prime, 0   // checkSPrimeS was invoked which means user input isn't a prime
				mov newinput, eax // new input is equal to eax so you can check if quotient is divisible again
				mov temp, 2    // reset temp to check up to new input
checkSPrime:	mov ebx, temp  // put temp in ebx so i can use it in manipulations
				cmp newinput, ebx // see if quotient is a prime again if newinput is less then temp then it is and no. is a sub prime
				jle coutput     // jumps output because we know no is second order prime
				mov eax, newinput // reintilize eax for doing next operations
				mov edx, 0     // resetting edx to be able to divide
				div temp	   // dividing newinput by current temp
				cmp edx, 0     // if there is no reminder 
				je notSPrime   // then newinput is not a second order prime so we jump to notSPrime
				add temp, 1    // otherwise increment temp so we can check to see if next can be divide into newinput
				mov sPrime, 1  // so far from what we checked currently newinput is not a second order prime
				jmp checkSPrime// loops it back to check for next no.
notSPrime:		mov sPrime, 0  // if you jmped to here then newinput is not a second order prime
coutput:			           //used to get to the end of the assembly code... 
		}
			//This condition just prints if user input is prime, second order prime, or neither
			if(prime == 1)
				printf("Input value %d is a prime.", cinput);
			else if(sPrime == 1)
				printf("Input value %d is a second order prime.", cinput);
			else 
				printf("Input value %d is neither a prime or a second order prime.", cinput);
		
			// Just in there  so we can see output....			
			scanf("%d", &cinput);
	
}